// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//


#pragma once

#include <cstdint>
#include <functional>
#include <string>
#include <unordered_set>

namespace kumo::pollux::exec::trace {
#define POLLUX_TRACE_LIMIT_EXCEEDED(errorMessage)                    \
  _POLLUX_THROW(                                                     \
      ::kumo::pollux::PolluxRuntimeError,                         \
      ::kumo::pollux::error_source::kErrorSourceRuntime.c_str(), \
      ::kumo::pollux::error_code::kTraceLimitExceeded.c_str(),   \
      /* isRetriable */ true,                                       \
      "{}",                                                         \
      errorMessage);

    /// The callback used to update and aggregate the trace bytes of a query. If the
    /// query trace limit is set, the callback return true if the aggregate traced
    /// bytes exceed the set limit otherwise return false.
    using UpdateAndCheckTraceLimitCB = std::function<void(uint64_t)>;

    struct TraceConfig {
        /// Target query trace nodes.
        std::unordered_set<std::string> queryNodes;
        /// Base dir of query trace.
        std::string queryTraceDir;
        UpdateAndCheckTraceLimitCB updateAndCheckTraceLimitCB;
        /// The trace task regexp.
        std::string taskRegExp;

        TraceConfig(
            std::unordered_set<std::string> _queryNodeIds,
            std::string _queryTraceDir,
            UpdateAndCheckTraceLimitCB _updateAndCheckTraceLimitCB,
            std::string _taskRegExp);
    };
} // namespace kumo::pollux::exec::trace
